package com.ficus.road.maintain.task;

import com.ficus.road.maintain.core.model.GpsHistory;
import com.ficus.road.maintain.dao.GpsHistoryDao;
import com.google.common.collect.Lists;
import com.opencsv.CSVReader;
import lombok.RequiredArgsConstructor;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

//@Component
@RequiredArgsConstructor
public class CvsInit {
    private final GpsHistoryDao gpsHistoryDao;

    @PostConstruct
    public void init() throws IOException {
        ClassPathResource classPathResource = new ClassPathResource("/all_jump_1.csv");
        File file = classPathResource.getFile();

        CSVReader reader;
        try {
            reader = new CSVReader(new FileReader(file));
            String[] line;
            reader.readNext();
            while ((line = reader.readNext()) != null) {
                GpsHistory gpsHistory = new GpsHistory();
                double lat = Double.parseDouble(line[4]);
                double lng = Double.parseDouble(line[3]);
                gpsHistory.setLatitude(lat);
                gpsHistory.setLongitude(lng);
                gpsHistory.setTimestamp(Long.parseLong(line[2].substring(0, 10)));
                gpsHistory.setLocation(Lists.newArrayList(lng, lat));
                gpsHistory.setCarId(line[0] + "_" + line[1]);
                gpsHistory.setIsFixed(false);
                gpsHistoryDao.insert(gpsHistory);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
